package com.wz.project.sap.STOCK_TRANSFER.controller;

import java.util.List;

import com.wz.project.sap.STOCK_TRANSFER.domain.TransferStock;
import com.wz.project.sap.STOCK_TRANSFER.domain.ZflWmsStockTransferHeader;
import com.wz.project.sap.STOCK_TRANSFER.domain.ZflWmsStockTransferItem;
import com.wz.project.sap.STOCK_TRANSFER.domain.ZflWmsStockTransferSeri;
import com.wz.project.sap.STOCK_TRANSFER.service.IZflWmsStockTransferItemService;
import com.wz.project.util.StringUtil;
import com.wz.project.util.UtilId;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.wz.framework.aspectj.lang.annotation.Log;
import com.wz.framework.aspectj.lang.enums.BusinessType;

import com.wz.framework.web.controller.BaseController;
import com.wz.framework.web.domain.AjaxResult;
import com.wz.common.utils.poi.ExcelUtil;
import com.wz.framework.web.page.TableDataInfo;

/**
 * 库存调拨回传Controller
 * 
 * @author wz
 * @date 2024-01-19
 */
@Controller
@RequestMapping("/sap/StockTransfer")
public class ZflWmsStockTransferItemController extends BaseController
{
    private String prefix = "sap/StockTransfer";

    @Autowired
    private IZflWmsStockTransferItemService zflWmsStockTransferItemService;

    @GetMapping()
    public String StockTransfer()
    {
        return prefix + "/StockTransfer";
    }

    /**
     * 查询库存调拨回传列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZflWmsStockTransferItem zflWmsStockTransferItem)
    {
        startPage();
        List<ZflWmsStockTransferItem> list = zflWmsStockTransferItemService.selectZflWmsStockTransferItemList(zflWmsStockTransferItem);
        return getDataTable(list);
    }

    /**
     * 导出库存调拨回传列表
     */
    @Log(title = "库存调拨回传", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZflWmsStockTransferItem zflWmsStockTransferItem)
    {
        List<ZflWmsStockTransferItem> list = zflWmsStockTransferItemService.selectZflWmsStockTransferItemList(zflWmsStockTransferItem);
        ExcelUtil<ZflWmsStockTransferItem> util = new ExcelUtil<ZflWmsStockTransferItem>(ZflWmsStockTransferItem.class);
        return util.exportExcel(list, "StockTransfer");
    }

    /**
     * 新增库存调拨回传
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存库存调拨回传
     */
    @Log(title = "库存调拨回传", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZflWmsStockTransferItem zflWmsStockTransferItem)
    {
        return toAjax(zflWmsStockTransferItemService.insertZflWmsStockTransferItem(zflWmsStockTransferItem));
    }

    /**
     * 修改库存调拨回传
     */
    @GetMapping("/edit/{ZEILE}")
    public String edit(@PathVariable("ZEILE") String ZEILE, ModelMap mmap)
    {
        ZflWmsStockTransferItem zflWmsStockTransferItem = zflWmsStockTransferItemService.selectZflWmsStockTransferItemById(ZEILE);
        mmap.put("zflWmsStockTransferItem", zflWmsStockTransferItem);
        return prefix + "/edit";
    }

    /**
     * 修改保存库存调拨回传
     */
    @Log(title = "库存调拨回传", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZflWmsStockTransferItem zflWmsStockTransferItem)
    {
        return toAjax(zflWmsStockTransferItemService.updateZflWmsStockTransferItem(zflWmsStockTransferItem));
    }

    /**
     * 删除库存调拨回传
     */
    @Log(title = "库存调拨回传", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zflWmsStockTransferItemService.deleteZflWmsStockTransferItemByIds(ids));
    }

    @Log(title = "库存调拨回传", businessType = BusinessType.UPDATE)
    @PostMapping( "/reloadType")
    @ResponseBody
    public AjaxResult reloadType(ZflWmsStockTransferItem zflWmsStockTransferItem)
    {
        return toAjax(zflWmsStockTransferItemService.updateZflWmsStockTransferType(zflWmsStockTransferItem));
    }

    @RequestMapping("/selectProduct")
    public String selectProduct() {

        return "transferStock/selectProduct";
    }

    @GetMapping("/addTransfer1")
    public String addTransfer1()
    {
        return "transferStock/addTransfer1";
    }


    @Log(title = "库存调拨回传", businessType = BusinessType.INSERT)
    @PostMapping( "/addSave1")
    @ResponseBody
    public AjaxResult addSave1(@RequestBody TransferStock transferStock)
    {
        ZflWmsStockTransferHeader zflWmsStockTransferHeader = transferStock.getZflWmsStockTransferHeader();
        List<ZflWmsStockTransferItem> list = transferStock.getZflWmsStockTransferItemList();
        List<ZflWmsStockTransferSeri> list1 = transferStock.getZflWmsStockTransferSeriList();
        String tran_guid = UtilId.uuids();
        zflWmsStockTransferHeader.setTranGuid(tran_guid);
        zflWmsStockTransferHeader.setEvType("0");
        zflWmsStockTransferItemService.insertZflWmsStockTransferHeader(zflWmsStockTransferHeader);
        for(int i = 0;i < list.size();i++){
            ZflWmsStockTransferItem item = list.get(i);
            for(int j = 0;j<list1.size();j++){
                ZflWmsStockTransferSeri seri = list1.get(j);
                if(Integer.parseInt(seri.getZEILE()) == Integer.parseInt(item.getZEILE())){
                    seri.setZEILE(String.valueOf(i+1));
                    seri.setTran_guid(tran_guid);
                    zflWmsStockTransferItemService.insertZflWmsStockTransferSeri(seri);
                }
            }
            item.setZEILE(String.valueOf(i+1));
            item.setTranGuid(tran_guid);
            if(item.getMATNR() != null && item.getMATNR().length() != 0) {
                item.setMATNR(StringUtil.Stringadd(item.getMATNR()));
            }
            if(item.getMatnrTo() != null && item.getMatnrTo().length() != 0){
                item.setMatnrTo(StringUtil.Stringadd(item.getMatnrTo()));
            }
            if(item.getAUFNR() != null && item.getAUFNR().length() != 0){
                item.setAUFNR(StringUtil.Stringadd12(item.getAUFNR()));
            }
            zflWmsStockTransferItemService.insertZflWmsStockTransferItem(item);
        }


        return AjaxResult.success("保存成功！");
    }

}
